﻿using DryIoc;
using Grpc.Client;
using MaterialDesignThemes.Wpf;
using Prism.DryIoc;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using Sigma.Admin.Client.Extensions;
using Sigma.Admin.Client.DialogAware;
using Sigma.Admin.Client.GrpcService;
using Sigma.Admin.Client.Services;
using Sigma.Admin.Client.ViewModels;
using Sigma.Admin.Client.ViewModels.DialogVM;
using Sigma.Admin.Client.Views;
using Sigma.Admin.Client.Views.Dialog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Sigma.Admin.Client
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
        #region 服务注册
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            #region 注册请求服务
            containerRegistry.GetContainer()
                .Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));
            containerRegistry.GetContainer().RegisterInstance(@"http://localhost:5069/", serviceKey: "webUrl");
            #endregion


            containerRegistry.RegisterForNavigation<MainWindow, MainWindowViewModel>();
            containerRegistry.RegisterForNavigation<IndexView, IndexViewModel>();
            containerRegistry.RegisterForNavigation<MenuRegionView, MenuRegionViewModel>();
            containerRegistry.RegisterForNavigation<MenuView, MenuViewModel>();
            containerRegistry.RegisterForNavigation<UsersView, UsersViewModel>();
            containerRegistry.RegisterForNavigation<RolesView, RolesViewModel>();
            containerRegistry.RegisterForNavigation<PermissionView, PermissionViewModel>();
            containerRegistry.RegisterForNavigation<OxyChartView, OxyChartViewModel>();

            #region 注册服务
            containerRegistry.Register<IDialogHostService, DialogHostService>();
            containerRegistry.Register<ILoginService, LoginService>();
            containerRegistry.Register<IMenuService, MenuService>();
            containerRegistry.Register<IUserService, UserService>();
            containerRegistry.Register<IPermissionService, PermissionService>();
            #endregion

            containerRegistry.RegisterSingleton<DrawerHost>();
            #region 注册弹窗
            containerRegistry.RegisterDialog<LoginView, LoginViewModel>();
            containerRegistry.RegisterForNavigation<ShowTipsView, ShowTipsViewModel>();
            containerRegistry.RegisterForNavigation<AddUserView, AddUserViewModel>();
            containerRegistry.RegisterForNavigation<AddMenuView, AddMenuViewModel>();
            #endregion


            #region 注册grpc服务provider
            containerRegistry.RegisterSingleton(typeof(GrpcServiceProvider));//隧道最好重用
            containerRegistry.Register(typeof(ChartService));
            #endregion
        }
        #endregion

        #region 登录界面
        protected override void OnInitialized()
        {
            var dialog = Container.Resolve<IDialogService>();
            var ea = Container.Resolve< IEventAggregator> ();
            var boolres = dialog == null;

            dialog.ShowDialog("LoginView", callback =>
            {
                //cancel 
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                ea.SendMessage("","UpdateMenu");
                var service = App.Current.MainWindow.DataContext as IConfigureService;
                if (service != null)
                    service.Configure();
                base.OnInitialized();
            });
        }

        public static void LoginOut(IContainerProvider containerProvider, IRegionManager regionManager)
        {
            Current.MainWindow.Hide();
            AppSession.Clear();
            var dialog = containerProvider.Resolve<IDialogService>();

            dialog.ShowDialog("LoginView", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }

                Current.MainWindow.Show();
            });
            regionManager.RequestNavigate("ContentRegion",nameof(IndexView));
        }
        #endregion
    }
}
